package member

import (
	"lao-a/controllers"
	mem "lao-a/models/member"
	"lao-a/models/model"
	"net/http"
)

// MemberController 应用
type MemberController struct {
	dao *mem.DAO
	controllers.BaseController
}

// Constructor 初始化 Controller
// @Title Constructor
// @Description 初始化 Controller, 系统自动调用
func (c *MemberController) Constructor() {
	c.dao = mem.NewDAO(c.Context)
}

// List 查询会员列表
func (c *MemberController) List() {
	mems, err := c.dao.GetMemberList()

	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	c.ResponseJson(map[string]interface{}{
		"Members": mems,
	})
}

// Get 查询会员
func (c *MemberController) Get() {
	memId := c.GetString(":memId")
	mem, err := c.dao.GetMemberById(memId)

	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	points, e1 := c.dao.GetPointsList(mem.AccId)
	if e1 != nil {
		c.ResponseJson(e1, http.StatusInternalServerError)
	}

	cash, e2 := c.dao.GetCashList(mem.AccId)
	if e2 != nil {
		c.ResponseJson(e2, http.StatusInternalServerError)
	}

	liveness, e3 := c.dao.GetLivenessList(mem.AccId)
	if e3 != nil {
		c.ResponseJson(e3, http.StatusInternalServerError)
	}

	invite, e4 := c.dao.GetInviteList(mem.MemId, mem.MemType)
	if e4 != nil {
		c.ResponseJson(e4, http.StatusInternalServerError)
	}

	c.ResponseJson(map[string]interface{}{
		"Member":       mem,
		"PointsList":   points,
		"CashList":     cash,
		"LivenessList": liveness,
		"InviteList":   invite,
	})
}

// Current 获取当前会员
func (c *MemberController) Current() {
	memId, _ := c.GetSession("PlatID").(string)
	if memId == "" {
		c.ResponseJson("请退出重试", http.StatusBadRequest)
	}

	c.Ctx.Input.SetParam(":memId", memId)
	c.Get()
}

// EditCurrent 编辑当前
func (c *MemberController) EditCurrent() {
	memId, _ := c.GetSession("PlatID").(string)
	if memId == "" {
		c.ResponseJson("请退出重试", http.StatusBadRequest)
	}

	c.Ctx.Input.SetParam(":memId", memId)
	c.Update()
}

// Update
func (c *MemberController) Update() {
	mem := model.TaMember{}
	if err := c.ParseForm(&mem); err != nil {
		c.ResponseJson("接受参数失败", http.StatusInternalServerError)
	}

	mem.MemId = c.GetString(":memId")

	cols := []string{
		"name",
		"nicky_name",
		"avatar",
		"id_card",
		"passport",
		"birthday",
		"email",
		"phone",
		"address",
		"interests",
		"profession",
		"disease_history",
		"remark",
		"status",
		"src_type",
		"src_id",
	}

	if err := c.dao.UpdateMember(&mem, cols); err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	c.ResponseJson("ok")
}

// Save 保存会员
func (c *MemberController) Save() {
	mem, err := c.dao.InitMember(c.Input())

	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	c.ResponseJson(map[string]interface{}{
		"Member": mem,
	})
}

// Join 入会
func (c *MemberController) Join() {
	mem, err := c.dao.JoinClub(c.Input())

	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	c.ResponseJson(map[string]interface{}{
		"Member": mem,
	})
}

// GetMemberTypes 查询会员类型
func (c *MemberController) GetMemberTypes() {
	memberTypes := c.dao.GetMemberTypes()

	c.ResponseJson(map[string]interface{}{
		"MemberTypes": memberTypes,
	})
}

// GetMemberLevels 查询会员d等级
func (c *MemberController) GetMemberLevels() {
	memberlvls, err := c.dao.GetMemberNormalLvl()
	if err != nil {
		c.ResponseJson(err, http.StatusInternalServerError)
	}

	c.ResponseJson(map[string]interface{}{
		"MemberLevels": memberlvls,
	})
}
